package cn.iocoder.yudao.module.game.service.withdrawnRecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.withdrawnRecord.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.withdrawnRecord.WithdrawnRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.withdrawnRecord.WithdrawnRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 待提现 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WithdrawnRecordServiceImpl implements WithdrawnRecordService {

    @Resource
    private WithdrawnRecordMapper withdrawnRecordMapper;

    @Override
    public Long createWithdrawnRecord(WithdrawnRecordSaveReqVO createReqVO) {
        // 插入
        WithdrawnRecordDO withdrawnRecord = BeanUtils.toBean(createReqVO, WithdrawnRecordDO.class);
        withdrawnRecordMapper.insert(withdrawnRecord);
        // 返回
        return withdrawnRecord.getId();
    }

    @Override
    public void updateWithdrawnRecord(WithdrawnRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateWithdrawnRecordExists(updateReqVO.getId());
        // 更新
        WithdrawnRecordDO updateObj = BeanUtils.toBean(updateReqVO, WithdrawnRecordDO.class);
        withdrawnRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteWithdrawnRecord(Long id) {
        // 校验存在
        validateWithdrawnRecordExists(id);
        // 删除
        withdrawnRecordMapper.deleteById(id);
    }

    private void validateWithdrawnRecordExists(Long id) {
        if (withdrawnRecordMapper.selectById(id) == null) {
            throw exception(WITHDRAWN_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public WithdrawnRecordDO getWithdrawnRecord(Long id) {
        return withdrawnRecordMapper.selectById(id);
    }

    @Override
    public PageResult<WithdrawnRecordDO> getWithdrawnRecordPage(WithdrawnRecordPageReqVO pageReqVO) {
        return withdrawnRecordMapper.selectPage(pageReqVO);
    }

}